D:\a\scloud-dns\scloud-dns\src\dns\zones\mod.rs
Line | Count | Source |
1 | | pub(crate) mod zone_parser; |
2 | | |
3 | | use crate::dns::records::DNSRecord; |
4 | | use std::collections::HashMap; |
5 | | |
6 | | /// Represents a DNS zone. |
7 | | /// |
8 | | /// A `Zone` contains all resource records associated with a DNS zone, |
9 | | /// typically loaded from a zone file. It includes metadata such as the |
10 | | /// origin, default TTL, SOA record, and all other records grouped by name. |
11 | | /// |
12 | | /// This structure is commonly used by authoritative DNS servers and |
13 | | /// zone file parsers. |
14 | | /// |
15 | | /// - `origin`: Optional zone origin (e.g. "example.com.") |
16 | | /// - `name`: Zone name |
17 | | /// - `ttl`: Default TTL for records in the zone |
18 | | /// - `soa`: Start of Authority (SOA) record, if present |
19 | | /// - `records`: DNS records indexed by owner name |
20 | | /// |
21 | | /// # Exemple : |
22 | | /// ``` |
23 | | /// use std::collections::HashMap; |
24 | | /// use crate::dns::zone::Zone; |
25 | | /// use crate::dns::records::DNSRecord; |
26 | | /// use crate::dns::q_type::DNSRecordType; |
27 | | /// use crate::dns::q_class::DNSClass; |
28 | | /// |
29 | | /// let soa = DNSRecord { |
30 | | /// name: "example.com".to_string(), |
31 | | /// rtype: DNSRecordType::SOA, |
32 | | /// rclass: DNSClass::IN, |
33 | | /// ttl: 3600, |
34 | | /// value: "ns1.example.com hostmaster.example.com".to_string(), |
35 | | /// priority: None, |
36 | | /// weight: None, |
37 | | /// port: None, |
38 | | /// flags: None, |
39 | | /// tag: None, |
40 | | /// regex: None, |
41 | | /// replacement: None, |
42 | | /// order: None, |
43 | | /// preference: None, |
44 | | /// }; |
45 | | /// |
46 | | /// let mut records = HashMap::new(); |
47 | | /// records.insert( |
48 | | /// "example.com".to_string(), |
49 | | /// vec![DNSRecord { |
50 | | /// name: "example.com".to_string(), |
51 | | /// rtype: DNSRecordType::A, |
52 | | /// rclass: DNSClass::IN, |
53 | | /// ttl: 300, |
54 | | /// value: "93.184.216.34".to_string(), |
55 | | /// priority: None, |
56 | | /// weight: None, |
57 | | /// port: None, |
58 | | /// flags: None, |
59 | | /// tag: None, |
60 | | /// regex: None, |
61 | | /// replacement: None, |
62 | | /// order: None, |
63 | | /// preference: None, |
64 | | /// }], |
65 | | /// ); |
66 | | /// |
67 | | /// let zone = Zone { |
68 | | /// origin: Some("example.com.".to_string()), |
69 | | /// name: "example.com".to_string(), |
70 | | /// ttl: 3600, |
71 | | /// soa: Some(soa), |
72 | | /// records, |
73 | | /// }; |
74 | | /// |
75 | | /// assert_eq!(zone.name, "example.com"); |
76 | | /// ``` |
77 | | #[derive(Debug, PartialEq)] |
78 | | pub struct Zone { |
79 | | pub origin: Option<String>, |
80 | | pub name: String, |
81 | | pub ttl: u32, |
82 | | pub soa: Option<DNSRecord>, |
83 | | pub records: HashMap<String, Vec<DNSRecord>>, |
84 | | } |
85 | | |
86 | | impl Zone { |
87 | | /// Get all DNS records for a given name. |
88 | | /// |
89 | | /// The name must match the record owner name exactly as stored |
90 | | /// in the zone (usually a fully-qualified domain name). |
91 | | /// |
92 | | /// # Exemple : |
93 | | /// ``` |
94 | | /// use crate::dns::zone::Zone; |
95 | | /// use crate::dns::records::DNSRecord; |
96 | | /// |
97 | | /// let records = zone.get_records("example.com"); |
98 | | /// |
99 | | /// if let Some(records) = records { |
100 | | /// assert!(!records.is_empty()); |
101 | | /// } |
102 | | /// ``` |
103 | | #[allow(unused)] |
104 | 0 | pub fn get_records(&self, name: &str) -> Option<&Vec<DNSRecord>> { |
105 | 0 | self.records.get(name) |
106 | 0 | } |
107 | | } |